using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IAC_2013.Forms;
using IAC_2013.Controls;
using DevComponents.AdvTree;
using DevComponents.DotNetBar.Metro.ColorTables;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar;
using System.Diagnostics;

namespace IAC_2013
{
    public partial class frmMain : MetroAppForm
    {

        MetroBillCommands _Commands = null;
        StartControl _StartControl = null;

        public frmMain()
        {
            InitializeComponent();

            _Commands = new MetroBillCommands();

            _Commands.Inventory.New = new Command(components);
            _Commands.Inventory.New.Executed += newInventoryExecuted;
            _Commands.Inventory.Cancel = new Command(components);
            _Commands.Inventory.Cancel.Executed += CancelInventoryExecuted;
            _Commands.Inventory.Save = new Command(components);
            _Commands.Inventory.Save.Executed += SaveInventoryExecuted;

            _Commands.InvoiceIn.New = new Command(components);
            _Commands.InvoiceIn.New.Executed += newInvoiceInExecuted;
            _Commands.InvoiceIn.Cancel = new Command(components);
            _Commands.InvoiceIn.Cancel.Executed += CancelInvoiceInExecuted;

            _Commands.Partners.New = new Command(components);
            _Commands.Partners.New.Executed += newPartnersExecuted;
            _Commands.Partners.Cancel = new Command(components);
            _Commands.Partners.Cancel.Executed += CancelPartnerExecuted;

            _Commands.Products.New = new Command(components);
            _Commands.Products.New.Executed += newProductsExecuted;
            _Commands.Products.Cancel = new Command(components);
            _Commands.Products.Cancel.Executed += CancelProductsExecuted;

            _Commands.BaseUnit.New = new Command(components);
            _Commands.BaseUnit.New.Executed += newBaseUnitExecuted;
            _Commands.BaseUnit.Cancel = new Command(components);
            _Commands.BaseUnit.Cancel.Executed += CancelBaseUnitExecuted;

            this.SuspendLayout();
            _StartControl = new StartControl();
            _StartControl.Commands = _Commands;
            panelSlide.Controls.Add(_StartControl);
            _StartControl.BringToFront();
            _StartControl.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Right;
            _StartControl.Click += new EventHandler(StartControl_Click);
            this.ResumeLayout(false);
        }

        ctrBaseUnit _baseUnit = null;
        private void newBaseUnitExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_baseUnit == null);
            _baseUnit = new ctrBaseUnit();
            _Commands.BaseUnit.New.Enabled = false;
            _baseUnit.Commands = _Commands;
            this.ShowModalPanel(_baseUnit, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            if (!_baseUnit.Visible)
            {
                _StartControl.SlideOutButtonVisible = false;
            }
        }

        private void CancelBaseUnitExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_baseUnit != null);
            CloseBaseUnitDialog();
        }

        private void CloseBaseUnitDialog()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            this.CloseModalPanel(_baseUnit, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            _baseUnit.Commands = null;
            _baseUnit = null;
            _Commands.BaseUnit.New.Enabled = true;
        }

        ctrProduct _products = null;
        private void newProductsExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_products == null);
            _products = new ctrProduct();
            _Commands.Products.New.Enabled = false;
            _products.Commands = _Commands;
            this.ShowModalPanel(_products, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            if (!_products.Visible)
            {
                _StartControl.SlideOutButtonVisible = false;
            }
        }

        private void CancelProductsExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_products != null);
            CloseProductsDialog();
        }

        private void CloseProductsDialog()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            this.CloseModalPanel(_products, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            _products.Commands = null;
            _products.Dispose();
            _products = null;
            _Commands.Products.New.Enabled = true;
        }

        Partners _partners = null;
        private void newPartnersExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_partners == null);
            _Commands.Partners.New.Enabled = false;
            _partners = new Partners();
            _partners.Commands = _Commands;
            this.ShowModalPanel(_partners, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelPartnerExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_partners != null);
            ClosePartnerDialog();
        }

        private void ClosePartnerDialog()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            this.CloseModalPanel(_partners, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            _partners.Commands = null;
            _partners.Dispose();
            _partners = null;
            _Commands.Partners.New.Enabled = true;
        }

        Inventory _inventory = null;
        private void newInventoryExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_inventory == null);
            _Commands.Inventory.New.Enabled = false;
            _inventory = new Inventory();
            _inventory.Commands = _Commands;
            this.ShowModalPanel(_inventory, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;

        }


        private void CancelInventoryExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_inventory != null);
            CloseInventoryDialog();
        }

        InvoiceIn _invoiceIn = null;
        private void newInvoiceInExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_invoiceIn == null);
            _Commands.InvoiceIn.New.Enabled = false;
            _invoiceIn = new InvoiceIn();
            _invoiceIn.Commands = _Commands;
            
            this.ShowModalPanel(_invoiceIn, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelInvoiceInExecuted(object sender, EventArgs e)
        {
            Debug.Assert(_invoiceIn != null);
            CloseInvoiceInDialog();
        }

        private void CloseInvoiceInDialog()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            this.CloseModalPanel(_invoiceIn, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            _invoiceIn.Commands = null;
            _invoiceIn.Dispose();
            _invoiceIn = null;
            _Commands.InvoiceIn.New.Enabled = true;
        }

        private void CloseInventoryDialog()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            this.CloseModalPanel(_inventory, DevComponents.DotNetBar.Controls.eSlideSide.Right);
            _inventory.Commands = null;
            _inventory.Dispose();
            _inventory = null;
            _Commands.Inventory.New.Enabled = true;
        }

        private void SaveInventoryExecuted(object sender, EventArgs e)
        { 
        
        }

        private void CancelCtrCurrentCyExecuted(object sender, EventArgs e)
        {
            ICommandSource source = (ICommandSource)sender;
            MetroColorGeneratorParameters theme = (MetroColorGeneratorParameters)source.CommandParameter;
            StyleManager.MetroColorGeneratorParameters = theme;
        }

        private void SaveCtrCurrentCyExecuted(object sender, EventArgs e)
        { 
        
        }

        private void StartControl_Click(object sender, EventArgs e)
        {
            _StartControl.IsOpen = false;
        }

        private void treeSystem_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeSystem.SelectedNode.Index == 0)
            {
                MessageBox.Show("Chức năng đang hoàn thiện.");
            }
            else if (treeSystem.SelectedNode.Index == 1) 
            {
                frmCurrentcy frm = new frmCurrentcy();
                frm.ShowDialog();
            }
            else if (treeSystem.SelectedNode.Index == 2)
            {
                MessageBox.Show("Chức năng đang hoàn thiện.");
            }
            else if (treeSystem.SelectedNode.Index == 3)
            {
                MessageBox.Show("Chức năng đang hoàn thiện.");
            }
            else if (treeSystem.SelectedNode.Index == 4)
            {
                MessageBox.Show("Chức năng đang hoàn thiện.");
            }
            else if (treeSystem.SelectedNode.Index == 5)
            {
                MessageBox.Show("Chức năng đang hoàn thiện.");
            }
            else if (treeSystem.SelectedNode.Index == 6)
            {
                frmUser users = new frmUser();
                users.ShowDialog();
            }
        }

        private void lbUnit_Click(object sender, EventArgs e)
        {
            frmUnit frm = new frmUnit();
            frm.ShowDialog();
        }

        private void treeProduct_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeProduct.SelectedNode.Index == 0)
            {
                frmProductCategory frm = new frmProductCategory();
                frm.ShowDialog();
            }
            else if (treeProduct.SelectedNode.Index == 1)
            {
                MessageBox.Show("Chức năng đang hoàn thiện.");
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn đóng chương trình làm việc?", "Đóng lại", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                e.Cancel = false;
            else
                e.Cancel = true;
        }

    }
}